<?php

namespace app\api\controller;

use app\common\validate\SmsValidate;

use think\Request;

class Sms
{

    const NOT_PARAM = '缺少参数';

    protected $code = '';

    /**
     * 获取短信验证码
     */
    public function sendSms(Request $request){
        $param = $request->param();
        if (empty($param['phone'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'phone 不能为空');
        }

        (new SmsValidate())->goCheck();
        /*if($param['client_key'] != config('sms.client_key')){
            return show(config('status.ERROR_STATUS'), '客户端密钥错误', '客户端密钥错误');
        }*/
        $smsObj = new \app\lib\Sms();
        $result = $smsObj->send($param['phone']);

        if($result['msg'] == 'ok'){
            $this->code = $result['code'];
            return show(config('status.SUCCESS_STATUS'), '短信发送成功', $result);
        }else{
            return show(config('status.ERROR_STATUS'), '短信发送失败', '短信发送失败');
        }
    }

    /**
     * 验证短信验证码
     */
    public function checkSmsCode(Request $request){
        $param = $request->param();
        if (empty($param['code'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'code 不能为空');
        }

        if($param['code'] == session('sms_code')){
            return show(config('status.SUCCESS_STATUS'), '验证码正确', '验证码正确');
        }else{
            return show(config('status.ERROR_STATUS'), '验证码错误', '验证码错误');
        }
    }

}